package com.kupurui.asteacher.ui.job;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.util.Toolkit;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.PreviewAnswerAdapter;
import com.kupurui.asteacher.adapter.PreviewCompletionAdapter;
import com.kupurui.asteacher.adapter.PreviewRadioAdapter;
import com.kupurui.asteacher.bean.PreviewInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.utils.UserManger;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import info.hoang8f.widget.FButton;
import okhttp3.Call;
import okhttp3.Response;


/**
 * 已选题目
 * Created by Administrator on 2017/7/24.
 */

public class PreviewAty extends BaseAty {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.fbtn_push)
    FButton fbtnPush;

    List<PreviewInfo> radioList;
    PreviewRadioAdapter radioAdapter;//单选

    private boolean isAll = true;

//    private List<String> listID;
    private List<String> questionID;//传到布置作业的问题ID

    @Override
    public int getLayoutId() {
        return R.layout.preview_aty;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tvRight.setSelected(true);
    }

    @Override
    public void initData() {
//        listID = new ArrayList<>();
//        if (getIntent().getExtras() != null) {
//            listID = getIntent().getStringArrayListExtra("id");
//        }
        questionID = new ArrayList<>();
        radioList = new ArrayList<>();
        radioAdapter = new PreviewRadioAdapter(this,radioList,R.layout.preview_radio_item);
        listview.setAdapter(radioAdapter);
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().zuoye_yulan(UserManger.subject_id, this, 0);
    }

    @OnClick({R.id.iv_back, R.id.tv_right, R.id.fbtn_push})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right://全选
                if (tvRight.isSelected()) {
                    isAll = false;
                    tvRight.setSelected(false);
                } else {
                    isAll = true;
                    tvRight.setSelected(true);
                }
                all();
                break;
            case R.id.fbtn_push://布置作业
                questionID.clear();
                for (int i = 0;i<radioList.size();i++){
                    if (radioList.get(i).getIs_selector()){
                        questionID.add(radioList.get(i).getQuestion().getQ_id());
                    }
                }
                if (Toolkit.listIsEmpty(questionID)){
                    showToast("请选择你要发布的题目");
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("id", (ArrayList<String>) questionID);
                if (UserManger.PUSH_TYPE == 1){
                    bundle.putString("type","2");
                    startActivity(PreReleaseAty.class, bundle);
                }else if (UserManger.PUSH_TYPE == 2){
                    startActivity(JobPushAty.class, bundle);
                }
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what) {
            case 0:
                radioList.clear();
                radioList.addAll(AppJsonUtil.getArrayList(result,PreviewInfo.class));
                radioAdapter.notifyDataSetChanged();
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void all() {
        if (isAll) {
            for (int i = 0; i < radioList.size(); i++) {
                radioList.get(i).setIs_selector(true);
            }
        } else {
            for (int i = 0; i < radioList.size(); i++) {
                radioList.get(i).setIs_selector(false);
            }
        }
        radioAdapter.notifyDataSetChanged();
    }

}
